package com.ruoyi.web.controller.util;

import com.alibaba.fastjson.JSONObject;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import static org.junit.Assert.assertEquals;


public class KxtSmsUtil {
    private static String account = "mt9272";//mt3569
    private static String password = "ho7i1x";

    public static JSONObject sendSms(String mobiles, String code, String type) {
        int respCode = 0;
        long t1 = System.currentTimeMillis();
        long totaltime = 0;
        String smscontent = "";
        if (type.equals("0")) {
            smscontent = "您的本次校验码为:" + code + "(3分钟内有效),请勿泄漏给他人。如非本人操作，请忽略本条消息！";
        } else if (type.equals("1")) {
            smscontent = "请确保该操作是您本人进行，您的校验码是:" + code + ",该校验码3分钟内有效。";
        } else if (type.equals("2")) {
            smscontent = "请确保该操作是您本人进行，您的校验码是:" + code + ",该校验码3分钟内有效。";
        }
        HttpURLConnection connection = null;
        JSONObject jsonOs = new JSONObject();
        try {
            String url = UrlConst.sendSms;
            URL postUrl = new URL(url);
            // 打开连接
            long totalt1 = System.currentTimeMillis();
            connection = (HttpURLConnection) postUrl.openConnection();
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setRequestMethod("POST");
            connection.setUseCaches(false);
            connection.setInstanceFollowRedirects(true);
            connection.setRequestProperty("Content-Type",
                    "application/x-www-form-urlencoded");
            connection.connect();
            DataOutputStream out = new DataOutputStream(
                    connection.getOutputStream());
            String timestamp = Long.toString(System.currentTimeMillis());
            StringBuffer access_token = new StringBuffer();
            access_token.append(timestamp).append(password);
            String access_tokenS = MD5.md5(access_token.toString());

            StringBuffer s1 = new StringBuffer();
            s1.append("account=").append(account).append("&timestamp=")
                    .append(timestamp);
            s1.append("&access_token=").append(access_tokenS)
                    .append("&receiver=").append(mobiles)
                    .append("&smscontent=")
                    //.append(Base64.encode(smscontent.getBytes("GBK")))
                    .append(java.net.URLEncoder.encode(smscontent, "UTF-8"))
                    .append("&extcode=");
            out.writeBytes(s1.toString());
            out.flush();
            out.close();
            long totalt2 = System.currentTimeMillis();
            totaltime = totaltime + (totalt2 - totalt1);
            System.out.println(connection.getResponseCode()
                    + "-----------------");
            respCode = connection.getResponseCode();
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    connection.getInputStream(), "UTF-8"));// 设置编码,否则中文乱码
            String lines = reader.readLine();

            if (lines != null) {
                jsonOs = JSONObject.parseObject(lines);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                connection.disconnect();
            }
        }
        long t2 = System.currentTimeMillis();
        System.out.println("耗时......" + totaltime);
        System.out.println("耗时,包括取连接......" + (t2 - t1));
        assertEquals(respCode, 200);
        return jsonOs;
    }

}
